package com.wobito.controller.operate;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wobito.common.constants.BaseResponse;
import com.wobito.common.constants.BusinessException;
import com.wobito.common.constants.ErrorCode;
import com.wobito.common.utils.DateUtils;
import com.wobito.common.utils.ResultUtils;
import com.wobito.pojo.bean.DeviceLog;
import com.wobito.pojo.bean.Person;
import com.wobito.pojo.dto.person.PersonInfoDTO;
import com.wobito.pojo.result.PersonResult;
import com.wobito.service.IBorrowReturnLogService;
import com.wobito.service.IBorrowService;
import com.wobito.service.IDeviceLogService;
import com.wobito.service.IPersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author Wangbaba
 * @Date 2024/9/2 10:05
 * @Version 1.0
 */
@RestController
@RequestMapping("/operate/person")
@Api(tags = "个人信息操作接口")
public class PersonalController {

    @Autowired
    private IPersonService personService;
    @Autowired
    private IBorrowService borrowService;
    @Autowired
    private IDeviceLogService deviceLogService;
    @Autowired
    private IBorrowReturnLogService borrowReturnLogService;

    @GetMapping("/personReminder")
    @ApiOperation(value = "个人借阅临期提醒", notes = "刷卡或刷人脸后展示借阅已逾期或即将到期的借阅情况，有逾期显示逾期，无逾期显示临期提醒（只显示7天以内）")
    public BaseResponse personReminder(@RequestParam(value = "code") String code) {
        Person person = personService.getOne(new LambdaQueryWrapper<Person>().eq(Person::getIcCode, code));
        if (ObjectUtil.isEmpty(person)) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "未查询到个人信息,请检查IC卡或重置人脸");
        }
        deviceLogService.insertDeviceLog(new DeviceLog(null, DateUtils.getNowDate(), person.getId(), "个人借阅临期提醒"));
        return ResultUtils.success(borrowService.selectBorrowReminderList(person.getId()));
    }

    @GetMapping("/getPerson")
    @ApiOperation(value = "查看个人信息", notes = "根据IC卡编号或人脸获取")
    public BaseResponse getPerson(@RequestParam(value = "code") String code) {
        Person person = personService.getOne(new LambdaQueryWrapper<Person>().eq(Person::getIcCode, code));
        if (ObjectUtil.isEmpty(person)) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "未查询到个人信息,请检查IC卡或重置人脸");
        }
        PersonResult personResult = new PersonResult();
        //个人信息
        personResult.setPersonInfo(BeanUtil.copyProperties(person, PersonInfoDTO.class));
        //借阅情况
        personResult.setBorrowByPersons(borrowService.selectBorrowByPerson(person.getId()));
        //借阅日志
        personResult.setBorrowLogByPersons(borrowReturnLogService.selectBookBorrowReturnByPersonId(person.getId()));
        deviceLogService.insertDeviceLog(new DeviceLog(null, DateUtils.getNowDate(), person.getId(), "查看个人信息"));
        return ResultUtils.success(personResult);
    }
}
